package com.wlz.util;

import com.sun.xml.internal.ws.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 公共方法
 * </p>
 *
 * @author liqi
 * @since 2020-09-17
 */
@Slf4j
public class CommonUtil {

    /**
     * 初始化
     */
    private CommonUtil() {
    }

    /**
     * 获取相对路径
     */
    public static String resolveFilePath(String path) {
        return "/" + path;
    }

    /**
     * 获取文件MD5
     */
    public static String getFileMd5(String path) {
        try {
            return DigestUtils.md5Hex(new FileInputStream(path));
        } catch (Exception ex) {
            log.error("获取文件MD5失败:", ex);
            return null;
        }
    }

    /**
     * 导入错误数据
     */
    public static Map<String, Object> setFailedMap(String value1, String value2) {
        HashMap<String, Object> hashMap = new HashMap<>(2);
        hashMap.put("field", value1);
        hashMap.put("remark", value2);
        return hashMap;
    }

    /**
     * 根据属性名获取属性值
     */
    public static Object getFieldValueByName(Object obj, String fieldName) {
        try {
            if (obj == null) {
                return "";
            } else if (obj instanceof String) {
                return obj;
            }
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = obj.getClass().getMethod(getter);
            Object properyObj = method.invoke(obj);
            if (properyObj == null) {
                return "";
            }
            return properyObj;
        } catch (Exception ex) {
            log.error("根据属性名获取属性值失败,错误信息为:", ex);
            return null;
        }
    }

    /**
     * 正则校验,返回所有括号中的内容
     */
    public static List<String> getFieldNameByRegex(String content) {
        List<String> list = new ArrayList<>();
        Pattern pattern = Pattern.compile("(?<=\\{)[^\\}]+");
        Matcher m = pattern.matcher(content);
        while (m.find()) {
            list.add(m.group());
        }
        return list;
    }

    /**
     * 正则校验
     */
    public static boolean checkRegular(String input, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        return matcher.matches();
    }

    /**
     * 正则校验,返回匹配的字符串
     */
    public static List<String> getRegularGroup(String input, String regex) {
        List<String> list = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(input);
        while (m.find()) {
            list.add(m.group());
        }
        return list;
    }

    /**
     * 除法方法
     *
     * @param value1 除数
     * @param value2 被除数
     * @param scale  小数点后保留位
     * @return
     */
    public static BigDecimal divide(BigDecimal value1, BigDecimal value2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("scale必须大于等于0");
        }
        if (value2.compareTo(new BigDecimal(0)) <= 0) {
            return new BigDecimal(0);
        }
        //调用除法方法,小数点后保留b位,并将结果进行四舍五入操作
        return value1.divide(value2, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 除法方法
     *
     * @param value1 除数
     * @param value2 被除数
     * @param scale  小数点后保留位
     * @return
     */
    public static BigDecimal divide(BigDecimal value1, Integer value2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("scale必须大于等于0");
        }
        if (value2.compareTo(0) <= 0) {
            return new BigDecimal(0);
        }
        BigDecimal b2 = new BigDecimal(value2.toString());
        //调用除法方法,小数点后保留b位,并将结果进行四舍五入操作
        return value1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 右对齐
     */
    public static String padRight(String src, int len, char ch) {
        int diff = len - src.length();
        if (diff <= 0) {
            return src;
        }

        char[] charr = new char[len];
        System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
        for (int i = 0; i < diff; i++) {
            charr[i] = ch;
        }
        return new String(charr);
    }

    /**
     * 校验终端固件版本(二期业务)
     * -1 - 终端固件版本为空
     * 0  - 终端固件版本为0
     * 1  - 终端固件版本为一期流程
     * 2  - 终端固件版本为二期流程
     * 3  - 终端固件版本为二期架构三流程
     */
    public static int checkFirmwareVersion(String firmwareVersion) {
//        if (StringUtils.isBlank(firmwareVersion)) {
//            return -1;
//        }
        if (firmwareVersion.equals("0")) {
            return 0;
        }
        String regex = "^T4[A-Z]{1}BJ[XPF]{1}[0-9]{2}$";
        if (checkRegular(firmwareVersion, regex)) {
            return 3;
        }
        String regex1 = "^T4[A-Z]{1}BJ([ETZVWN]{1}[0-9]{2})$";
        if (checkRegular(firmwareVersion, regex1)) {
            if (!firmwareVersion.substring(5).equals("T07")) {
                return 2;
            }
        }
        return 1;
    }
}
